package net.xju_springJdbc11_12.dao;

import net.xju_springJdbc11_12.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

@Repository
@Transactional
public class StudentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    public List<Student> getStudents() {
        String sql = "select * from newtable";
        BeanPropertyRowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
        return jdbcTemplate.query(sql, rowMapper);
    }
    @Transactional(timeout = 2,rollbackFor = Exception.class)
    public void insert(Student student) throws FileNotFoundException {

//        try {
//            Thread.sleep(4000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        String sql = "insert into newtable(name, sn) values(?,?)";
        jdbcTemplate.update(sql, student.getName(), student.getSn());
        new FileInputStream("xx");

//        int a = 10 / 0;
    }
}
